package com.todolist.controller;


import com.result.Result;
import com.todolist.dao.User;
import com.todolist.dao.TTodolist;
import com.todolist.mapper.TTodolistMapper;
import com.todolist.service.ToDOListService;
import com.todolist.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 员工管理
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户相关接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ToDOListService toDOListService;

    /**
     * 登录
     *
     * @param
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Result<User> login(@RequestBody User user) {
        log.info("用户登录：{}", user);
        User result = userService.login(user);

        return Result.success(result);
    }

    /**
     * 注册用户
     *
     * @param
     * @return
     */
    @PostMapping("/register")
    @ApiOperation(value = "注册用户")
    public Result<String> register(@RequestBody User user) {
        log.info("注册用户：{}", user);
        userService.regitser(user);

        return Result.success("成功");
    }

    /**
     * 查找代办事项
     * @param
     * @return
     */
    @GetMapping("/getToDo/{username}")
    @ApiOperation(value = "查找代办事项")
    public Result<List<TTodolist>> getToDo(@PathVariable String username){
        log.info("查找代办事项：{}", username);
        List<TTodolist> listByName =  toDOListService.getListByName(username);

        return Result.success(listByName);
    }

    /**
     * 根据当前事项id去设置事项状态
     * @param
     * @return
     */
    @PostMapping("/setToDo")
    @ApiOperation(value = "根据当前事项id去设置事项状态")
    public Result<String> setToDo(@RequestBody TTodolist todolist){
        log.info("根据当前事项id去设置事项状态：{}", todolist);
         toDOListService.setStatusByID(todolist);

        return Result.success("成功");
    }

    /**
     * 根据事项id去删除事项
     * @param
     * @return
     */
    @DeleteMapping("/deleteToDo/{id}")
    @ApiOperation(value = "根据事项id去删除事项")
    public Result<String> deleteToDo(@PathVariable int id){
        log.info("根据事项id去删除事项：{}", id);
        toDOListService.delToDoById(id);

        return Result.success("成功");
    }

    /**
     * 清除已完成的事项
     * @param
     * @return
     */
    @DeleteMapping("/deleteHasDo/{username}")
    @ApiOperation(value = "根据用户名称清除已完成的事项")
    public Result<String> deleteHasDo(@PathVariable String username){
        log.info("根据用户名称清除已完成的事项：{}", username);
        toDOListService.delAllDoByUsername(username);

        return Result.success("成功");
    }

}
